package com.sky.dao;

import java.io.File;
import java.lang.reflect.Field;
import java.sql.SQLException;
import java.util.List;

import javax.activation.FileDataSource;
import javax.enterprise.inject.New;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.sky.domain.User;
import com.sky.utils.DBUtils01;

public class UserDao {

	public boolean insertUserToDB(User user) throws SQLException {
		// 数据库插入操作
		QueryRunner queryRunner = new QueryRunner(DBUtils01.getDataSource());
		String sql = "insert into user (email,username,password,gender,telephone,"
				+ "introduce,activeCode) values (?,?,?,?,?,?,?)";
		int rowCount = queryRunner.update(sql, user.getEmail(), user.getUsername(), user.getPassword(),
				user.getGender(), user.getTelephone(), user.getIntroduce(), user.getActiveCode());

		if (rowCount > 0) {
			return true;
		}
		return false;
	}

	public boolean updateActiveCodeState(String code) throws SQLException {
		// TODO Auto-generated method stub
		QueryRunner queryRunner = new QueryRunner(DBUtils01.getDataSource());
		String sql="update user set state = ?  where activeCode = ?";
		int rowCount = queryRunner.update(sql,1,code);
		if (rowCount > 0) {
			return true;
		}
		return false;
	}
	//根据指定邮箱查找用户，如果用户存在，说明邮箱已经被注册过了，
	public User  queryUserByEmail(String email) throws SQLException{
		QueryRunner queryRunner = new QueryRunner(DBUtils01.getDataSource());
		String sql="select * from user where email = ?";
		//参数2：rsh：ResultSetHandler(接口)：结果集处理   ，反射   
		//new BeanHandler<User>(User.class):表示把查询到的结果映射到实体类对应的变量中(反射)，
		User user = queryRunner.query(sql, new BeanHandler<User>(User.class),email);
		 return user;	
	}
	
	public  User  queryUserByUserNameAndPwd(User user) throws SQLException{
		QueryRunner queryRunner = new QueryRunner(DBUtils01.getDataSource());
		String sql="select * from user where username =? and password = ?";
		User user2 =queryRunner.query(sql, new BeanHandler<User>(User.class),user.getUsername(),user.getPassword());
		return user2;
	}
	
	public  String  queryPwdbyUserName(String username)throws SQLException{
		QueryRunner queryRunner = new QueryRunner(DBUtils01.getDataSource());
		String sql="select password from user where username = ? ";
		Object object = queryRunner.query(sql, new ScalarHandler(),username);
		return object.toString();
	}

	public int updatePwd(User user)throws SQLException {
		// TODO Auto-generated method stub
		QueryRunner queryRunner = new QueryRunner(DBUtils01.getDataSource());
		String sql="update user set  password=  ?  where username = ? ";
		int rowCount  = queryRunner.update(sql, user.getPassword(),user.getUsername());
		return rowCount;
	}

	public List<User> queryUserList()throws SQLException {
		// TODO Auto-generated method stub
		QueryRunner queryRunner = new QueryRunner(DBUtils01.getDataSource());
		String sql="select * from user";
		return queryRunner.query(sql, new BeanListHandler<User>(User.class));
		
	}

	public int  deleteUserById(String id)throws SQLException  {
		// TODO Auto-generated method stub
		QueryRunner queryRunner = new QueryRunner(DBUtils01.getDataSource());
		String sql="delete  from user  where id = ?";
		int rowCount = queryRunner.update(sql,id);
		return rowCount;
	}

	//模糊查询，三种格式：
	//例如：关键是a  
	//查询以a开头    a%; %任意字符(没有长度限制)   _代表一位字符       
	//查询以a结尾   %a
	//查询含有a的内容   %a%  情况使用较多
	//备注：sql语句中值类型为string,必须用单引号引起来，java代码语句不需要用单引号
	public List<User> queryUserByKey(String keyString)throws SQLException {
		QueryRunner queryRunner = new QueryRunner(DBUtils01.getDataSource());
		String sql="select * from user where username like ?";
		return	queryRunner.query(sql, new BeanListHandler<User>(User.class),"%"+keyString+"%");
	}

	//超
	public int updateUserById(User user) throws SQLException {
		// TODO Auto-generated method stub
		int rowCount=-1;
		QueryRunner queryRunner = new QueryRunner(DBUtils01.getDataSource());
//判断imgurl是否为空 为空不修改
		if (null == user.getImgurl() || "".equals(user.getImgurl())) {
			
			String sql = "update user set username = ?,gender = ?,telephone = ?,introduce =?  where id=  ?";
			rowCount = queryRunner.update(sql, user.getUsername(), user.getGender(), user.getTelephone(),
					user.getIntroduce(),  user.getId());
		} else {
			String sql = "update user set username = ?,gender = ?,telephone = ?,introduce =?,imgurl=?  where id=  ?";
			rowCount = queryRunner.update(sql, user.getUsername(), user.getGender(), user.getTelephone(),
					user.getIntroduce(), user.getImgurl(), user.getId());
		}
		return rowCount;
	}
//根据ID查用户信息
public User queryUserById(String id) throws SQLException {
		QueryRunner queryRunner = new QueryRunner(DBUtils01.getDataSource());
		String sql = "select * from user where id= ?";
		User user2 = queryRunner.query(sql, new BeanHandler<User>(User.class), id);
		return user2;
	}
}
